<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :before-close="closeDailog"
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="操作信息 备注" prop="note">
                <el-input v-model="dataForm.note" placeholder="操作信息 备注" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="操作类型 导入|导出" prop="operateType">
                <el-input v-model="dataForm.operateType" placeholder="操作类型 导入|导出" maxlength="30" show-word-limit>
                </el-input>
            </el-form-item>
            <el-form-item label="状态 进行中|已成功" prop="status">
                <el-input v-model="dataForm.status" placeholder="状态 进行中|已成功" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="所属集团" prop="pkGroup">
                <el-input v-model="dataForm.pkGroup" placeholder="所属集团" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="所属组织" prop="pkOrg">
                <el-input v-model="dataForm.pkOrg" placeholder="所属组织" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="乐观锁" prop="revision">
                <el-input v-model="dataForm.revision" placeholder="乐观锁" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="创建人" prop="createdBy">
                <el-input v-model="dataForm.createdBy" placeholder="创建人" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createdTime">
                <el-input v-model="dataForm.createdTime" placeholder="创建时间" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="删除标志 Y/N" prop="deleFlag">
                <el-input v-model="dataForm.deleFlag" placeholder="删除标志 Y/N" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="批次" prop="batch">
                <el-input v-model="dataForm.batch" placeholder="批次" maxlength="30" show-word-limit></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog()">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as datarecordService from '@/api/report/datarecord'

    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    id: 0,
                    note: '',
                    operateType: '',
                    status: '',
                    pkGroup: '',
                    pkOrg: '',
                    revision: '',
                    createdBy: '',
                    createdTime: '',
                    deleFlag: '',
                    batch: ''
                },
                dataRule: {
                    note: [{
                        required: true,
                        message: '操作信息 备注不能为空',
                        trigger: 'blur'
                    }],
                    operateType: [{
                        required: true,
                        message: '操作类型 导入|导出不能为空',
                        trigger: 'blur'
                    }],
                    status: [{
                        required: true,
                        message: '状态 进行中|已成功不能为空',
                        trigger: 'blur'
                    }],
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'blur'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'blur'
                    }],
                    revision: [{
                        required: true,
                        message: '乐观锁不能为空',
                        trigger: 'blur'
                    }],
                    createdBy: [{
                        required: true,
                        message: '创建人不能为空',
                        trigger: 'blur'
                    }],
                    createdTime: [{
                        required: true,
                        message: '创建时间不能为空',
                        trigger: 'blur'
                    }],
                    deleFlag: [{
                        required: true,
                        message: '删除标志 Y/N不能为空',
                        trigger: 'blur'
                    }],
                    batch: [{
                        required: true,
                        message: '批次不能为空',
                        trigger: 'blur'
                    }]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        datarecordService.getDataRecordInfo(this.dataForm.id).then(data => {
                            this.dataForm.note = data.note
                            this.dataForm.operateType = data.operateType
                            this.dataForm.status = data.status
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.revision = data.revision
                            this.dataForm.createdBy = data.createdBy
                            this.dataForm.createdTime = data.createdTime
                            this.dataForm.deleFlag = data.deleFlag
                            this.dataForm.batch = data.batch
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'id': this.dataForm.id || undefined,
                            'note': this.dataForm.note,
                            'operateType': this.dataForm.operateType,
                            'status': this.dataForm.status,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'revision': this.dataForm.revision,
                            'createdBy': this.dataForm.createdBy,
                            'createdTime': this.dataForm.createdTime,
                            'deleFlag': this.dataForm.deleFlag,
                            'batch': this.dataForm.batch
                        }
                        var tick = !this.dataForm.id ? datarecordService.addDataRecord(params) :
                            datarecordService.updateDataRecord(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            }
        }
    }
</script>